import { Entity, Column, PrimaryColumn, ManyToOne, JoinColumn, CreateDateColumn } from 'typeorm';
import { User } from '../../users/entities/user.entity';
import { Device } from '../../devices/entities/device.entity';

@Entity('device_audit_codes')
export class DeviceAuditCode {
  @PrimaryColumn({ length: 64 })
  auditNumber: string;

  @Column({ length: 64 })
  verifyCode: string;

  @CreateDateColumn()
  createdAt: Date;

  @Column({ nullable: true })
  usedAt: Date;

  @Column({ nullable: true, length: 64 })
  deviceSerialNumber: string;

  @Column()
  createdBy: string;

  @Column({ default: false })
  isDeleted: boolean;

  // Relations - with foreign key constraints disabled
  @ManyToOne(() => User, { createForeignKeyConstraints: false })
  @JoinColumn({ name: 'created_by' })
  creator: User;

  @ManyToOne(() => Device, { nullable: true, createForeignKeyConstraints: false })
  @JoinColumn({ name: 'device_serial_number', referencedColumnName: 'serialNumber' })
  device: Device;
}
